package cn.sccl.service.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;

import cn.sccl.dao.CustomerDao;
import cn.sccl.dao.impl.CustomerDaoImpl;
import cn.sccl.domain.Customer;
import cn.sccl.service.CustomerService;
import cn.sccl.utils.HibernateUtils;

/**
 * @author 饶爽
 * @time 2019年8月24日 下午3:50:20
 * @version 1.0.0
 * CustomerService实现类
*/
public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = new CustomerDaoImpl();

    @Override
    public void save(Customer customer) {
    	//获取绑定当前线程的session
    	Session session = HibernateUtils.getCurrentSession();
    	//开启事务
    	Transaction tx = session.beginTransaction();
        //调用DAO层保存客户信息
    	try {
    		customerDao.save(customer);
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
        tx.commit();
    }

	@Override
	public List<Customer> getAll() {
		//获取绑定当前线程的session
    	Session session = HibernateUtils.getCurrentSession();
    	//开启事务
    	Transaction tx = session.beginTransaction();
        //调用DAO层获取所有保存客户信息
    	List<Customer> list = customerDao.getAll();
		
        tx.commit();
		return list;
	}

    @Override
    public List<Customer> getAll(DetachedCriteria dc) {
        //获取绑定当前线程的session
        Session session = HibernateUtils.getCurrentSession();
        //开启事务
        Transaction tx = session.beginTransaction();
        //调用DAO层获取所有保存客户信息
        List<Customer> list = customerDao.getAll(dc);
        
        tx.commit();
        return list;
    }

}
